package com.jinkaimen.goodcash.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.BaseAdapter;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseRefreshListViewActivity;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.goodcash.bean.GetShopList;
import com.jinkaimen.goodcash.bean.ShopBean;
import com.jinkaimen.goodcash.dao.AccountDao;
import com.jinkaimen.goodcash.manager.ApiManager;
import com.jinkaimen.goodcash.ui.adapter.ShopManagerAdapter;
import com.jinkaimen.goodcash.utils.URLUtils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class ShopManagerActivity extends BaseRefreshListViewActivity<ShopBean> {

    private long mEditShopId;


    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("店铺管理");
    }

    @Override
    protected View getHeaderView() {
        return null;
    }

    @Override
    protected void initChildDataFromCache() {

    }

    @Override
    protected BaseAdapter getAdapter() {
        return new ShopManagerAdapter(this, mDataList);
    }

    @Override
    protected void load() {
        ApiManager.getApi().shopList(
                "HSY001010",
                AccountDao.getInstance().getAccount().id
        ).enqueue(new NetCallBack<GetShopList>() {
            @Override
            protected void onSuccess(Call<GetShopList> call, Response<GetShopList> response) {
                GetShopList getShopList = response.body();
                mDataList.addAll(getShopList.shopList);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            protected void hiddenProgress() {
                onRefreshCompleteSuccess();
            }
        });
    }

    @Override
    protected void onLoadMore(List<ShopBean> dataList) {
        onLoadMoreComplete();
    }

    //获取扫描的结果
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (intentResult != null) {
            if (intentResult.getContents() != null) {
                // ScanResult 为获取到的字符串
                String url = intentResult.getContents();
                String qrCode = URLUtils.getValueByName(url, "code");
                ApiManager.getApi().bindQrCode(
                        "HSY001020",
                        AccountDao.getInstance().getAccount().id,
                        qrCode,
                        mEditShopId
                ).enqueue(new NetCallBack<String>() {
                    @Override
                    protected void onSuccess(Call<String> call, Response<String> response) {
                        if (response.body() != null){
                            UIUtils.showToast("绑定二维码成功");
                            mPullToRefreshListView.doPullRefreshing(true, 0);
                        }
                    }

                    @Override
                    protected boolean isNullNeedCallback() {
                        return true;
                    }
                });
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }


    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected void onEvent(int eventCode, Object data) {
        switch (eventCode){
            case EventCode.EVENT_CODE_START_BIND_QRCODE_SHOP_ID:
                mEditShopId = (long) data;
                break;
        }
    }
}
